//
//  CustomShowAlertView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/6.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class CustomShowAlertView: UIView, BottomShadow, DelayExecution {
    private let maxWidth: CGFloat = ScreenWidth - 54 * 2
    private var tapClosure: (() -> Void)?
    private var doneOrCancelClosure: ((Bool) -> Void)?
    private var title: String = ""
    private var showTime: CGFloat = 0
    private var mainView = BaseCircleView()
    private var centerView = UIView()
    private let centerViewHeight: CGFloat = 91
    private let bottomViewHeight: CGFloat = 43
    private let mainViewHeight: CGFloat = 134
    private let samllViewHeight: CGFloat = 82
    private let samllViewLRMargin: CGFloat = 37
    private let mainViewWidth: CGFloat = 242
    private let topOffset: CGFloat = 40
    private let bottomView = UIView()
    
    /// 单按钮弹窗
    ///
    /// - Parameters:
    ///   - title: 描述内容
    ///   - tapClosure: 点击回调
    open func show(title: String, tapClosure: @escaping () -> Void) {
        self.title = title
        self.tapClosure = tapClosure
        setUI()
        showBottomView(yPoint: centerView.height)
        setCancelClick()
    }
    
    /// 确定或取消
    ///
    /// - Parameters:
    ///   - title: 描述
    ///   - doneOrCancelClosure: 点击回调
    open func show(title: String, doneOrCancelClosure: @escaping (Bool) -> Void) {
        self.title = title
        self.doneOrCancelClosure = doneOrCancelClosure
        setUI()
        showBottomView(yPoint: centerView.height)
        setDoneOrCancelButton()
    }
    
    /// 确定或取消
    ///
    /// - Parameters:
    ///   - title: 描述
    ///   - doneOrCancelClosure: 点击回调
    open func show(title: String, doneOrCancelClosure: @escaping () -> Void) {
        self.title = title
        setUI()
        showBottomView(yPoint: centerView.height)
        setDoneOrCancelButton()
    }
    
    /// 显示一个单纯的提示框
    ///
    /// - Parameters:
    ///   - title: 描述
    ///   - showTime: 显示时间
    ///   - completeClosure: 显示完成后的回调
   @objc public func show(title: String, showTime: CGFloat, completeClosure: @escaping () -> Void) {
        self.title = title
        self.showTime = showTime
        self.tapClosure = completeClosure
        setUI()
    }
    
    func showBottomView(yPoint: CGFloat) {
        bottomView.frame = CGRect(x: 0, y: yPoint, width: mainView.width, height: bottomViewHeight)
        bottomView.backgroundColor = ONColor.searchButtomColor
        mainView.addSubview(bottomView)
    }
    
    private func setUI() {
        let window = UIApplication.shared.keyWindow
        self.frame = window!.bounds
        self.alpha = 0.5
        self.backgroundColor = ONColor.maskBGColor
        window?.addSubview(self)
        UIView.animate(withDuration: TimeInterval(anTIME)) {
            self.alpha = 1
        }
        
        self.addSubview(mainView)
        mainView.backgroundColor = UIColor.white
        let textSize = MyConfigTool.myConfigWithLabelTextRect(with: CGSize(width: maxWidth, height: CGFloat(MAXFLOAT)), withFontSize: 17, withText: self.title).size
        let size = CGSize(width: textSize.width + samllViewLRMargin * 2, height: textSize.height + 35 * 2)
        
        if showTime > 0 {
            delayExecution(time: TimeInterval(showTime), closure: {
                self.hide()
            })
        }
        
        mainView.frame.size = size
        if showTime.isZero {
            mainView.height += bottomViewHeight
        }
        
        mainView.center = CGPoint(x: self.width / 2, y: self.height / 2 - topOffset)
        setShadow(view: mainView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        
        centerView.frame = CGRect(x: 0, y: 0, width: mainView.width, height: showTime > 0 ? mainView.height : mainView.height - bottomViewHeight)
        centerView.backgroundColor = UIColor.white
        mainView.addSubview(centerView)
        
        let titleLabel = UILabel()
        titleLabel.font = UIFont.systemFont(ofSize: 17)
        titleLabel.textColor = ONColor.normalTextColor
        titleLabel.textAlignment = .center
        titleLabel.text = title
        titleLabel.numberOfLines = 0
        titleLabel.sizeToFit()
        titleLabel.frame = CGRect(x: 0, y: 0, width: maxWidth, height: textSize.height + 1)
        
        titleLabel.center = CGPoint(x: centerView.width / 2, y: centerView.height / 2)
        centerView.addSubview(titleLabel)
    }
    
    /// 确认或取消
    private func setDoneOrCancelButton() {
        for index in 0...1 {
            let btnWidht: CGFloat = bottomView.width / 2
            let btn = UIButton(type: .custom)
            btn.frame = CGRect(x: CGFloat(index) * btnWidht, y: 0, width: btnWidht, height: bottomViewHeight)
            let offset = index == 0 ? -10 : 10
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, CGFloat(offset), 0, 0)
            btn.backgroundColor = UIColor.clear
            btn.addTarget(self, action: #selector(doneOrCancelTap(_ :)), for: .touchUpInside)
            btn.tag = index
            let imageName = index == 0 ? "icon_wrong" : "icon_determine"
            btn.setImage(UIImage(named: imageName), for: .normal)
            bottomView.addSubview(btn)
        }
    }
    
    /// 设置单个按钮
    private func setCancelClick() {
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: bottomView.width, height: bottomViewHeight)
        btn.backgroundColor = UIColor.clear
        btn.addTarget(self, action: #selector(singleclick), for: .touchUpInside)
        btn.setImage(UIImage(named: "common_prohibit"), for: .normal)
        bottomView.addSubview(btn)
    }
    
    // MARK: - 消失
    private func hide() {
        UIView.animate(withDuration: TimeInterval(anTIME), animations: {
            self.alpha = 0.2
        }) { (make) in
            if self.showTime > 0 {
                self.tapClosure?()
            }
            self.removeFromSuperview()
        }
    }
    
    //MARK: - 点击事件
    
    @objc func singleclick() {
        tapClosure?()
        hide()
    }
    
    @objc func doneOrCancelTap(_ btn: UIButton) {
        doneOrCancelClosure?(btn.tag != 0)
        hide()
    }
    
}
